﻿using System;
using System.Data;
using BO.Infrastructure.StaticData;
using GUI.Extensions;
using BO.News;
using System.Text;
using BO.Infrastructure.Utils;

namespace GUI.Controls.News
{
    public partial class CareableNews2 : System.Web.UI.UserControl
    {
        private const string Format =
            "<li>"
            + "<div class=\"clearfix relative\">"
                + "{3}{5}"
            + "</div>"
            + "<h2>"
               + "<a href=\"{0}\" title=\"{2}\" class=\"prop_title\">{1}</a>"
            + "</h2>"
            + "{4}"
        + "</li>";

        private const string PublishedDateFormat = "<p class=\"prop_publishinfo\">"
                                                   + "<b><span>{0}</span></b>"
                                                   + "</p>";

        private const string StateFormat = "<p class=\"state overlay\">"
                                           + "<span class=\"state-item sprite2 view-state\"></span><span>{0}</span> <a class=\"state-item sprite2 comment-state\"></a><span>{1}</span>"
                                           + "</p>";

        public int Top = 3;
        private const int ImageWidth = 320;
        private const int ImageHeight = 240;

        public int BoxId { get; set; }
        public string DisplayStyle { get; set; }
        public bool DisplayPublishedDate { get; set; }
        public bool DisplayState { get; set; }
        public bool GetDataFromNoCacheLayer = true;

       


        protected void Page_Load(object sender, EventArgs e)
        {
            var bo = GetDataFromNoCacheLayer && CacheSettingExtensions.EnableCacheHtml ? NewsBO_NoCache.GetInstance() : NewsBO.GetInstance();
            var objCat = RequestExtensions.Category;
            var zoneId = 0;
            if (objCat != null)
            {
                zoneId = objCat.ParentId > 0 ? objCat.ParentId : objCat.Id;
            }
            var dt = bo.GetEmbedNewsOnPage(Top, zoneId: zoneId, type: 2); //type cua box nhung chuyen muc
            Visible = DataBind(dt, DisplayPublishedDate, DisplayState);
        }

        private bool DataBind(DataTable dt, bool displayPublishedDate, bool displayState)
        {
            if (dt != null && dt.Rows.Count > 0)
            {
                var sb = new StringBuilder();
                foreach (DataRow r in dt.Rows)
                {
                    var avatar = PresentationUtils.Photo.BuildNewsAvatar(r, ImageWidth, ImageHeight, lazyLoad: true);
                    sb.AppendFormat(Format,
                        r[SqlConstants.NewsPublish.Url],
                        r[SqlConstants.NewsPublish.Title],
                        r[SqlConstants.NewsPublish.EncodedTitle],
                        avatar,
                        displayPublishedDate ? BuildPublishedDate(PresentationUtils.Object2DateTime(r[SqlConstants.NewsPublish.DistributionDate].ToString())) : string.Empty, //string.Empty,
                        displayState ? BuildState(r) : string.Empty
                        );
                }
                ltrData.Text = sb.ToString();
                return true;
            }
            return false;
        }

        private string BuildState(DataRow r)
        {
            return string.Format(StateFormat, 10000, 100);
        }

        private string BuildPublishedDate(DateTime publishedDate)
        {
            return string.Format(PublishedDateFormat, publishedDate.ToString("dd/MM/yyyy HH:mm"));
        }
    }
}